package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.SysRole;
import com.easy.common.bean.SysUser;
import com.easy.common.enumbean.UserStatus;
import com.easy.common.service.IUserService;
import com.easy.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class UserService implements IUserService{
@Autowired
UserMapper userMapper;
@Autowired
RoleService roleService;

    @Override
    public SysUser insert(SysUser user) {
        userMapper.insert(user);
        SysUser result=this.getById(user.getId());
        return result;
    }

    @Override
    public SysUser update(SysUser user) {
        userMapper.updateById(user);
        SysUser result=this.getById(user.getId());
        return result;
    }

    @Override
    public int delete(Integer id) {
        int num=userMapper.deleteById(id);
        return num;
    }

    @Override
    public SysUser getById(Integer id) {
        SysUser result=userMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<SysUser> getListByPage(IPage page, SysUser user) {
        Wrapper wrapper=new QueryWrapper(user);
        IPage result=userMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public List<SysUser> getList(SysUser user) {
        Wrapper wrapper=new QueryWrapper(user);
        List<SysUser> result=userMapper.selectList(wrapper);
        return result;
    }

    @Override
    public int setStatus(Integer id) {
        SysUser user=userMapper.selectById(id);
        UpdateWrapper<SysUser> wrapper=new UpdateWrapper();
        if (user.getStatus()== UserStatus.NORMAL){
            wrapper.set("status",UserStatus.DISABLE);
        }else{
            wrapper.set("status",UserStatus.NORMAL);
        }
        wrapper.eq("id",user.getId());
        return userMapper.update(wrapper);
    }

    @Override
    public SysUser assignRole(SysUser user) {
        // 删除用户原有的角色关联
        userMapper.deleteUserRoleByUserId(user.getId());

        // 插入新的角色关联
        List<Integer> roleIds = user.getRoleIds();
        if (roleIds != null && !roleIds.isEmpty()) {
            for (Integer roleId : roleIds) {
                userMapper.insertUserRole(user.getId(), roleId);
            }
        }

        // 返回更新后的用户信息
        return this.getById(user.getId());
    }

    @Override
    public SysUser getByUsername(String username) {
        SysUser user=userMapper.getOneByUsername(username);
        user.setRoles(getUserRoles(user));
        return user;
    }


    public List<SysRole> getUserRoles(SysUser user){
        List<SysRole> roles=userMapper.getRolesByUserID(user.getId());
        for(SysRole role:roles){
            roleService.getRolePermissions(role);
        }
        return roles;
    }


}
